package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohm_time_op.entity.dto.ShiftCapacityRecord4PageDTO;
import cn.lystudio.rohm_time_op.entity.dto.ShiftCapacityRecordDTO;
import cn.lystudio.rohm_time_op.entity.po.ShiftCapacityRecordPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.ShiftCapacityRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/shiftcaparecord")
@Slf4j
public class ShiftCapacityRecordController {
    private ShiftCapacityRecordService shiftCapacityRecordService;
    @Autowired
    @Lazy
    public void setShiftCapacityRecordService(ShiftCapacityRecordService shiftCapacityRecordService) {
        this.shiftCapacityRecordService = shiftCapacityRecordService;
    }

    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page,
                                        Integer limit,
                @RequestParam(value = "machineNo",defaultValue = "",required = false)String machineNo,
                @RequestParam(value = "searchDateRange",defaultValue = "",required = false)String searchDateRangeStr,
                @RequestParam(value="otherKeywords",defaultValue = "",required = false)String otherKeywords){
        // 1.参数整理
        String searchStartTime= StrUtil.EMPTY;
        String searchEndTime=StrUtil.EMPTY;
        if(StrUtil.isNotEmpty(searchDateRangeStr)){
            String[] tempArr = searchDateRangeStr.split("~|~");
            if(tempArr.length>=2){
                searchStartTime = DateUtil.format(DateUtil.parse(tempArr[0].trim()+" 00:00:00"),"yyyy-MM-dd HH:mm:ss");
                searchEndTime = DateUtil.format(DateUtil.parse(tempArr[1].trim()+" 00:00:00"),"yyyy-MM-dd HH:mm:ss");
            }
        }
        // 2.查询数据
        //PageUtil.setFirstPageNo(1);
        //List<ShiftCapacityRecordDTO> all = shiftCapacityRecordService.getAll(machineNo,searchStartTime,searchEndTime,otherKeywords);
        //if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);
        //return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
        // 3.分页查询
        ShiftCapacityRecord4PageDTO shiftCapacityRecord4PageDTO = shiftCapacityRecordService.getAll2( machineNo, searchStartTime, searchEndTime, otherKeywords,page, limit);
        if(CollUtil.isEmpty(shiftCapacityRecord4PageDTO.getShiftCapacityRecordDTOList())) return LayuiPageApiResponse.ofSuccessData(null,0);
        return LayuiPageApiResponse.ofSuccessData(shiftCapacityRecord4PageDTO.getShiftCapacityRecordDTOList(),shiftCapacityRecord4PageDTO.getAllSize());
    }

    @PostMapping("/delShiftCapacityRecord")
    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    public SaResult delShiftCapacityRecord(Integer shiftCapaRecordID){
        return SaResult.data(shiftCapacityRecordService.delShiftCapacityRecord(shiftCapaRecordID));
    }

    @PostMapping("/uptShiftCapacityRecord")
    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    public SaResult uptShiftCapacityRecord(@RequestBody ShiftCapacityRecordPO shiftCapacityRecordPO){
        return SaResult.data(shiftCapacityRecordService.uptShiftCapacityRecord(shiftCapacityRecordPO));
    }

    @GetMapping("/getDailyData")
    public LayuiPageApiResponse getDailyData(@RequestParam(value = "machineNo",defaultValue = "") String machineNo,
                                             @RequestParam(value = "dailyStart",defaultValue = "") String dailyStart,
                                             @RequestParam(value = "dailyEnd",defaultValue = "") String dailyEnd){
        if(StrUtil.isEmpty(machineNo) || StrUtil.isEmpty(dailyStart) || StrUtil.isEmpty(dailyEnd)){
            throw new JsonException(500,"请求数据异常");
        }
        return LayuiPageApiResponse.ofSuccessData(shiftCapacityRecordService.getDailyData(machineNo, dailyStart,dailyEnd),0);
    }

    @GetMapping("/getDailyData4Chart")
    @SaIgnore
    public SaResult getDailyData4Chart(@RequestParam("selectedMachineNo")String selectedMachineNo,
                                       @RequestParam("selectedDate")String selectedDate){
        if(StrUtil.isEmpty(selectedDate)||StrUtil.isEmpty(selectedMachineNo)){
            return SaResult.error("选择数据异常");
        }
        Date dtFrom = DateUtil.parse(selectedDate+" 07:00:00");

        Date dtTo = DateUtil.offsetDay(DateUtil.parse(selectedDate+" 07:00:00"),1);

        return SaResult.data(shiftCapacityRecordService.getDailyData4Chart(selectedMachineNo,DateUtil.format(dtFrom,"yyyy-MM-dd HH:mm:ss"),DateUtil.format(dtTo,"yyyy-MM-dd HH:mm:ss")));
    }

    @GetMapping("/getDailyData4Dashboard")
    @SaIgnore
    public LayuiPageApiResponse getDailyData4Dashboard(@RequestParam(value = "machineNo",defaultValue = "") String machineNo,
                                                       @RequestParam(value = "dailyStart",defaultValue = "") String dailyStart,
                                                       @RequestParam(value = "dailyEnd",defaultValue = "") String dailyEnd){
        if(StrUtil.isEmpty(machineNo) || StrUtil.isEmpty(dailyStart) || StrUtil.isEmpty(dailyEnd)){
            throw new JsonException(500,"请求数据异常");
        }
        return LayuiPageApiResponse.ofSuccessData(shiftCapacityRecordService.getDailyData4Dashboard(machineNo, dailyStart,dailyEnd),0);

    }

}
